package 新特征锁_取钱;

import java.util.concurrent.locks.Lock;

public class User implements Runnable {
    private String name; //用户名
    private Mycount myCount; //所要操作的账号
    private int iocash;//操作的金额，有正负之分
    private Lock myLock;//执行操作所需的锁对象

    User(String name,Mycount myCount,int iocash,Lock myLock){
        this.name = name;
        this.myCount = myCount;
        this.iocash = iocash;
        this.myLock = myLock;
    }

    @Override
    public void run() {
        myLock.lock();
        if(myCount.getCash() + iocash>0){
            System.out.println(name + "正在操作"+ myCount +"账号。金额为："+iocash+" ,当前金额为：" + myCount.getCash());
            myCount.setCash(myCount.getCash() + iocash);
            System.out.println(name + " 操作" + myCount +"账号成功。金额为"+ iocash+" ,当前金额为：" + myCount.getCash() );
        }
        else{
            System.out.println(name +"非法操作----------------");
        }
        myLock.unlock();
    }

    }


